<?php
include("../utility/connection.php");
include("../model/mUser.php");
session_start();

unset($_SESSION['message']);
unset($_SESSION['loginInfo']);

if($_SERVER["REQUEST_METHOD"] == "POST") {
    $LoginId = trim(mysql_real_escape_string($_POST["txtLoginId"]));
    $Password = trim(mysql_real_escape_string($_POST["txtPassword"]));
    
    $result = fnLogin($LoginId, $Password);
    $count = mysql_num_rows($result); // number of row
    $arrResult = mysql_fetch_array($result);
    
//    echo ('$LoginId: '.$LoginId.'\n');
//    echo ('$Password: '.$Password.'\n');
//    echo ('$count: '.$count.'\n');
    
    if ($count == 1) {
        $UserId = $arrResult[0];
        $Nama = $arrResult[1];
        $Alamat = $arrResult[2];
        $Telepon = $arrResult[3];
        $Email = $arrResult[4];
        $Login = $arrResult[5];
        $Password = $arrResult[6];
        $StsAdmin = $arrResult[7];
        $_SESSION['loginSts'] = '1';
        if ($StsAdmin == '1') {
            header("location: ../adminIndex.php");
        } else {
            $_SESSION['loginInfo'] = $arrResult;
            unset($_SESSION['message']);
            header("location: ../index.php");
        }
        
    } else {
        $message="Your Login or Password is Invalid";
        $_SESSION['message']=$message;
        unset($_SESSION['loginInfo']);
        unset($_SESSION['loginSts']);
        header("location: ../index.php");
    }
}

?>
